package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.dao.GreFileDao;
import cn.topevery.garden.dal.logic.GreFileLogic;
import cn.topevery.garden.dal.logic.GreTreeLogic;
import cn.topevery.garden.entity.dto.GreTreeDTO;
import cn.topevery.garden.entity.po.GreFile;
import cn.topevery.garden.entity.po.GreTree;
import cn.topevery.garden.entity.query.GreTreeQuery;
import cn.topevery.garden.entity.vo.GreTreeRecordVO;
import cn.topevery.garden.entity.vo.GreTreeVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.basis.dal.logic.SysDictLogic;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.dto.PointDto;
import hw.topevery.framework.enums.GeometryTypeEnum;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.topevery.garden.entity.po.GreFile.TYPE_GRE_TREE;

/**
 * 行道树
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Api(tags = "行道树")
@RestController
@RequestMapping("/greTree")
public class GreTreeController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    GreTreeLogic greTreeLogic;

    @Autowired
    GreFileLogic greFileLogic;

    @Autowired
    SysDictLogic sysDictLogic;

    @Autowired
    GreFileDao greFileDao;


    /**
     * 获取行道树分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取行道树页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<GreTreeVO>> listPage(@RequestBody GreTreeQuery query) {
        return JsonResult.ok(greTreeLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 行道树信息")
    @PostMapping("/insert")
    public JsonResult<Boolean> insert(@Valid @RequestBody GreTreeDTO dto) {
        GreTree greTree = mapper.map(dto, GreTree.class);
        greTree.setObjName("6");
        if (dto.getLng() != null && dto.getLat() != null) {
            String wkt = GeometryHelper.getWellKnownText(
                    Collections.singletonList(Collections.singletonList(new PointDto(dto.getLng(), dto.getLat()))), GeometryTypeEnum.POINT);
            greTree.setMapRanges(GeometryHelper.getGeometry(wkt));
            greTree.setGeoWkt(wkt);
        }
        greTreeLogic.saveOrUpdate(getUser().id.toString(), greTree);
        if (dto.getFileList() != null && !dto.getFileList().isEmpty()) {
            List<GreFile> collect = dto.getFileList().stream().peek(temp -> {
                temp.setDataType(TYPE_GRE_TREE);
                temp.setMainId(greTree.getId());
                temp.setMainRecordId(greTree.getId());
                temp.setDbStatus(false);
            }).collect(Collectors.toList());
            for (GreFile greFile : collect) {
                greFileDao.saveOrUpdate(getUser().id.toString(), greFile);
            }
        }
        return JsonResult.ok(true);
    }

    @ApiOperation("新增/编辑 行道树信息")
    @PostMapping("/update")
    public JsonResult<Boolean> update(@Valid @RequestBody GreTreeDTO dto) {
        greTreeLogic.saveOrUpdate(getUser().id.toString(), mapper.map(dto, GreTree.class));
        greFileDao.deleteByMainId(dto.getId(), getUser().id.toString(), TYPE_GRE_TREE);
        if (dto.getFileList() != null && !dto.getFileList().isEmpty()) {
            List<GreFile> collect = dto.getFileList().stream().peek(temp -> {
                temp.setDataType(TYPE_GRE_TREE);
                temp.setMainId(dto.getId());
                temp.setMainRecordId(dto.getId());
                temp.setDbStatus(false);
            }).collect(Collectors.toList());
            for (GreFile greFile : collect) {
                greFileDao.saveOrUpdate(getUser().id.toString(), greFile);
            }
        }
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<GreTreeVO> getInfoById(@RequestParam Integer id) {
        GreTree entity = greTreeLogic.getEntity(id);
        GreTreeVO vo = mapper.map(entity, GreTreeVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        greTreeLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出行道树信息")
    @GetMapping("/export")
    public void export(GreTreeQuery query, HttpServletResponse response) {
        List<GreTreeVO> list = greTreeLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "行道树", "sheet1", GreTreeVO.class, "行道树", response);
    }

    @PostMapping("/getList")
    @ApiOperation(value = "获取树木信息")
    public JsonResult<DataPager<GreTreeVO>> getList(@RequestBody GreTreeQuery query) {
        DataPager<GreTreeVO> res = greTreeLogic.getList(query);
        List<GreTreeVO> records = res.records;
        if (records != null && !records.isEmpty()) {
            records.stream().peek(temp -> {
                temp.setFileList(greFileLogic.getListByMainId(temp.getId(), TYPE_GRE_TREE));
            }).collect(Collectors.toList());
        }
        return JsonResult.ok(res);
    }

    @PostMapping(value = "/getRecordList")
    @ApiOperation(value = "获取行道树历史信息")
    public JsonResult<DataPager<GreTreeRecordVO>> getRecordList(@RequestBody GreTreeQuery query) {
        Map<String, Object> map = new HashMap<>();
        DataPager<GreTreeRecordVO> res = greTreeLogic.getRecordList(query);
        List<GreTreeRecordVO> records = res.records;
        if (records != null && !records.isEmpty()) {
            res.records = (records.stream().peek(temp -> {
                temp.setFileList(greFileLogic.getListByMainRecordId(temp.getId(), TYPE_GRE_TREE));
            }).collect(Collectors.toList()));
        }
        return JsonResult.ok(res);
    }

    @GetMapping(value = "/getById")
    @ApiOperation(value = "通过id获取树木信息")
    public JsonResult<GreTree> getById(@RequestParam Integer id) {
        GreTree res = greTreeLogic.getEntity(id);
        res.setFileList(greFileLogic.getListByMainId(res.getId(), TYPE_GRE_TREE));
        return JsonResult.ok(res);
    }

    @ApiOperation("excel数据导入")
    @PostMapping("/analysisExcel")
    public JsonResult<?> analysisExcel(@RequestBody MultipartFile file) {
        return greTreeLogic.analysisExcel(file);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importExcel")
    public JsonResult<?> importExcel(@RequestBody List<GreTreeVO> greTreeVOList) {
        return greTreeLogic.importExcel(greTreeVOList);
    }
}
